#include <stdlib.h>

#include "stack.h"

void stack_proverka(Stack *pstack)
{
        if(pstack->current == pstack->max_number)
        {
                Pointer *p = (Pointer *)malloc((pstack->max_number+ pstack->increment)*sizeof(Pointer));
                size_t i;
                for(i=1;i<pstack->max_number;i++)
                {
                        p[i]=pstack->info[i];
                }
                free(pstack->info);
                *pstack->info = *p;
                pstack->max_number = pstack->max_number + pstack->increment;
        }
}

void stack_create(Stack *pstack)
{
	pstack->increment    =50;
	pstack->initial_size = 50;	
	pstack->info		 = (Pointer *) malloc (pstack -> initial_size + 1*sizeof(Pointer));
	pstack->current         = 0;
	pstack->max_number     = pstack->initial_size;
}

void stack_destroy(Stack *pstack)
{
	free(pstack->info);
	pstack->current = NULL;
	pstack->max_number =NULL;
}

void stack_push(Stack *pstack, Pointer value)
{
        stack_proverka(pstack);

        pstack->info[pstack->current] = value;

        pstack->current++;
}

Pointer stack_pop(Stack *pstack)
{
        if(pstack->current == 0)
                return(0);
        pstack->current--;
        return(pstack->info[pstack->current]);
}

Pointer stack_peek(Stack *pstack)
{
        if(pstack->current == 0)
                return(0);
        return(pstack->info[pstack->current-1]);
}

size_t stack_size(Stack *pstack)
{
        return(pstack->current-1);
}

void stack_tune(Stack *pstack, size_t initial_size, size_t increment)
{
        pstack->initial_size = initial_size;
        pstack->increment    = increment;
}